#!/usr/bin/env python
'''
DocIndexer MP3 parser.
'''
from abstract_parser import AbstractParser
from common import EDocIndexer
import id3reader, PyLucene

class Parser(AbstractParser):

    description = 'MP3 music file parser'
    mime_types = ['audio/mpeg']

    def text(self,filename):
        try:
            id3r = id3reader.Reader(filename)
            self.title = id3r.getValue('title') or ''
            self.performer = id3r.getValue('performer') or ''
            self.album = id3r.getValue('album') or ''
            self.track = id3r.getValue('track') or ''
            result = u'%s\n%s\n%s\n%s' % (
                    self.title,
                    self.performer,
                    self.album,
                    self.track,
                )
            result = result.rstrip()
            if not result:
                raise EDocIndexer,'no ID3 information'
            return result
        except:
            raise EDocIndexer,'unable to parse document'


    def additional_fields(self):
        result = []
        result.append(PyLucene.Field('title', self.title,
               PyLucene.Field.Store.YES,
               PyLucene.Field.Index.TOKENIZED))
        result.append(PyLucene.Field('performer', self.performer,
               PyLucene.Field.Store.YES,
               PyLucene.Field.Index.TOKENIZED))
        result.append(PyLucene.Field('album', self.album,
               PyLucene.Field.Store.YES,
               PyLucene.Field.Index.TOKENIZED))
        result.append(PyLucene.Field('track', self.track,
               PyLucene.Field.Store.YES,
               PyLucene.Field.Index.UN_TOKENIZED))
        return result

if __name__ == '__main__':
    Parser.test()

# vim: ts=4 sw=4:

